﻿using System;

namespace EventBus2
{
    /// <summary>
    /// 观察者（订阅者）接口
    /// 规范了订阅的事件类型
    /// </summary>
    public abstract class ISubscriber<IEvent>
    {
        public abstract void Subscribe(Action<object, MyEventArgs> eventHandler);

    }

    /// <summary>
    /// 订阅者A
    /// </summary>
    public class SubscriberA : ISubscriber<EventA>
    {
        /// <summary>
        /// 订阅者应该知道自己要执行什么。当然也可以从外部修改
        /// </summary>
        public Action<object, MyEventArgs> eventHandler = (sender, eventArgs) =>
        {
            Console.WriteLine("ScbscriberA Handler   sender:" + sender.ToString() + "    eventArgs DateTime:" + eventArgs.DateTime.ToString());
        };

        /// <summary>
        /// 订阅事件
        /// </summary>
        public void Subscribe()
        {
            EventBus.Default.Subscribe<EventA>(eventHandler);
        }
        public override void Subscribe(Action<object, MyEventArgs> eventHandler)
        {
            EventBus.Default.Subscribe<EventA>(eventHandler);
        }
    }

    /// <summary>
    /// 订阅者B
    /// </summary>
    public class SubscriberB : ISubscriber<EventB>
    {
        /// <summary>
        /// 订阅者应该知道自己要执行什么。所以这里直接定义了一个Action.
        /// 当然也可以从外部传入Action
        /// </summary>
        public Action<object, MyEventArgs> eventHandler = (sender, eventArgs) =>
        {
            Console.WriteLine("ScbscriberB Handler   sender:" + sender.ToString() + "    eventArgs DateTime:" + eventArgs.DateTime.ToString());
        };

        public void Subscribe()
        {
            EventBus.Default.Subscribe<EventB>(eventHandler);
        }
        public override void Subscribe(Action<object, MyEventArgs> eventHandler)
        {
            EventBus.Default.Subscribe<EventB>(eventHandler);
        }

    }
}
